<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="searchInpt"
        placeholder="输入ID/昵称/手机号"
        clearable
        @keyup.enter.native="search()"
        class="search-btn"
      >
        <el-button
          type="primary"
          @click="search()"
          icon="el-icon-search"
          slot="append"
          :loading="searchLoading"
        >搜索</el-button>
      </el-input>
      <div class="lz-tips">
        <label>操作状态:</label>
      </div>
      <el-select
        v-model="exchangeStatusValue"
        placeholder="请选择状态"
        @change="getUnusualInfo()"
      >
        <el-option
          v-for="item in exchangeStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="table_container">
      <el-table
        border
        v-loading="tableloading"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          property="userId"
          label="id"
          align="center"
          width="100"
        >
        </el-table-column>
        <el-table-column
          property="userName"
          label="手机号"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="detectionTime"
          :formatter="formatterTime"
          label="最后检测时间"
          width="104"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="remark"
          label="异常原因"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="沟通方式列"
          align="center"
        >
          <template slot-scope="scope">
            <div
              style="display:inline-block;"
              v-for="(item,index) in scope.row.communicateWay"
              :key="index"
            >
              <span v-if="item == 0">
                &nbsp;未沟通&nbsp;
              </span>
              <span v-if="item == 1">
                &nbsp;电话&nbsp;
              </span>
              <span v-if="item == 2">
                &nbsp;短信&nbsp;
              </span>
              <span v-if="item == 3">
                &nbsp;微信&nbsp;
              </span>
              <span v-if="item == 4">
                &nbsp;App推送&nbsp;
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          property=""
          label="曲线"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="goGraph(scope.row)"
              type="text"
              size="small"
              v-show="permission_roles.customUserListCurve"
            >查看</el-button>
          </template>
        </el-table-column>

        <el-table-column
          label="处理操作"
          align="center"
          width="250"
        >
          <template slot-scope="scope">
            <el-button
              @click="changeCustomerHandleStatus(scope.row,2)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0 "
            >电话通知</el-button>
            <el-button
              @click="sendMessage(scope.row,2)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0"
            >APP通知</el-button>
            <el-button
              @click="sendMessage(scope.row,1)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0"
            >短信通知</el-button>
            <el-button
              @click="changeCustomerHandleStatus(scope.row,1)"
              type="text"
              size="small"
              v-if="scope.row.customerServiceHandle == 0"
            >无异常</el-button>
            <span v-if="scope.row.customerServiceHandle == 1">
              无异常
            </span>
            <span v-if="scope.row.customerServiceHandle == 2">
              已电话通知
            </span>
            <span v-if="scope.row.customerServiceHandle == 3">
              已APP通知
            </span>
            <span v-if="scope.row.customerServiceHandle == 4">
              已短信通知
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="发送消息"
      :visible.sync="showSendMessage"
      width="50%"
      center
    >
      <el-radio-group
        v-for="item in SMStemplateData"
        :key="item.templateCode"
        v-model="SMStemplateValue"
      >
        <ul class="SMS-ul">
          <li v-if="item.role == 1">
            <el-tooltip
              class="item"
              effect="dark"
              :content="item.context"
              placement="top-start"
            >
              <el-radio :label="item.templateCode">
                {{item.title}} : {{item.context}}
              </el-radio>
            </el-tooltip>
          </li>
        </ul>
      </el-radio-group>
      <p>
        推送用户:
      </p>
      <p>
        用户ID:{{sendMessageData.userId}} 手机号:{{sendMessageData.userName}} 昵称:{{sendMessageData.nickName}}
      </p>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="showSendMessage = false">取 消</el-button>
        <el-button
          type="primary"
          @click="doSendMessage()"
        >发 送</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getUnusualInfo, customerStatus } from "@/api/table";
import {
  fetchGetSMSTemplate,
  fetchSendSMS,
  fetchYouMengTemplateData,
  fetchYouMengPushData
} from "@/api/other";
import { formatDateTime } from "@/utils/common";
export default {
  data() {
    return {
      tableData: [],
      count: 0,
      limit: 20,
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      tableloading: true,
      exchangeStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "未操作",
          label: "未操作"
        },
        {
          value: "已操作",
          label: "已操作"
        }
      ],
      exchangeStatusValue: "",
      customerHandleDialog: false,
      //默认选择
      customerHandleStatus: "",
      //发送消息
      sendMsgBox: false,
      sendMsgData: {
        userId: "",
        msgConstent: ""
      },
      //弹框
      showSendMessage: false,
      sendMessageData: {},
      SMStemplateData: [],
      SMStemplateValue: "",
      SMStemplateStatus: "",
      sendChecked: true,
      pageshow: true
    };
  },
  mounted() {
    this.getUnusualInfo();
  },
  render: {},
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUnusualInfo();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getUnusualInfo();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    async getUnusualInfo() {
      this.tableloading = true;
      const Users = await getUnusualInfo({
        pageSize: this.limit,
        pageNo: this.currentPage,
        key: this.searchInpt,
        handleStatus: this.exchangeStatusValue
      });
      this.tableloading = false;
      this.tableData = [];
      this.count = Users.data.total;
      if (Users.data.list) {
        Users.data.list.forEach(item => {
          const tableData = {};
          tableData.userId = item.userId;
          tableData.userName = item.userName;
          tableData.nickName = item.nickName;
          tableData.detectionTime = item.detectionTime;
          tableData.prePregnant = item.prePregnant;
          tableData.communicateWay = item.communicateWay;
          tableData.count = item.count;
          tableData.physiologicalDataId = item.physiologicalDataId;
          tableData.customerServiceHandle = item.customerServiceHandle;
          tableData.status = item.status;
          tableData.remark = item.remark;
          this.tableData.push(tableData);
        });
      }
    },
    download(data) {
      location.href = ip1Conf + "/users/excel?userId=" + e.id;
    },
    async customerHandleData(data, i) {
      let radioStatus = "";
      if (i == 5) {
        radioStatus = 0;
      } else {
        radioStatus = i;
      }
      const res = await customerStatus({
        physiologicalDataId: data.physiologicalDataId,
        customerStatus: radioStatus
      });
      if (res.status == "200") {
        this.$message({
          type: "success",
          message: "修改成功"
        });
        this.getUnusualInfo();
      } else {
        this.$message({
          type: "error",
          message: res.msg
        });
      }
    },
    changeCustomerHandleStatus(data, i) {
      let confirmText = "";
      if (i == 0) {
        confirmText = "该条记录已处理，确定修改为未操作？";
      } else if (i == 1) {
        confirmText = "确定本次检测无异常？";
      } else if (i == 2) {
        confirmText = "确定已电话通知？";
      }

      this.$confirm(confirmText, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.customerHandleData(data, i);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    planStatus(data) {
      let planText = "";
      if (data.status) {
        planText = "确认将此用户改为已参加？";
      } else {
        planText = "确认将此用户改为未参加？";
      }
      this.$confirm(planText, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.planStatusData(data);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    goGraph(data) {
      this.$router.push({
        path: "/customerManage/serviceList/serviceChart",
        query: { t: +new Date(), type: 2, userId: data.userId }
      });
    },
    formatterTime(row) {
      if (row.detectionTime) {
        return formatDateTime(row.detectionTime);
      } else {
        return "";
      }
    },
    thousandStatus(value, row) {
      return row.status == value;
    },
    sendMsg(data) {
      this.$router.push({
        path: "sendMsg",
        query: { userId: data.physiologicalDataId, userName: data.userName }
      });
    },
    goMessageTemplate() {
      this.$router.push({ path: "serviceMessageTemplate", query: {} });
    },
    async sendMessage(data, i) {
      this.SMStemplateStatus = i;
      if (i == "1") {
        const SMStemplate = await fetchGetSMSTemplate();
        this.SMStemplateData = [];
        if (SMStemplate.status == "200") {
          SMStemplate.data.forEach(item => {
            const tableData = {};
            tableData.title = item.title;
            tableData.context = item.templateContext;
            tableData.templateCode = item.templateCode;
            tableData.role = item.role;
            this.SMStemplateData.push(tableData);
          });
          this.showSendMessage = true;
          this.sendMessageData = {};
          this.sendMessageData.userId = data.userId;
          this.sendMessageData.userName = data.userName;
          this.sendMessageData.nickName = data.nickName;
          this.sendMessageData.physiologicalDataId = data.physiologicalDataId;
        }
      } else if (i == "2") {
        const SMStemplate = await fetchYouMengTemplateData();
        this.SMStemplateData = [];
        if (SMStemplate.status == "200") {
          SMStemplate.data.forEach(item => {
            const tableData = {};
            tableData.title = item.title;
            tableData.context = item.templateContext;
            tableData.templateCode = item.id;
            tableData.role = item.role;
            this.SMStemplateData.push(tableData);
          });
          this.showSendMessage = true;
          this.sendMessageData = {};
          this.sendMessageData.userId = data.userId;
          this.sendMessageData.userName = data.userName;
          this.sendMessageData.nickName = data.nickName;
          this.sendMessageData.physiologicalDataId = data.physiologicalDataId;
        }
      }
    },
    async doSendMessage() {
      if (this.sendChecked) {
        if (this.SMStemplateValue == "") {
          this.$message({
            type: "error",
            message: "请选择短信模版"
          });
          return;
        }
        this.sendChecked = false;
        if (this.SMStemplateStatus == "1") {
          const res = await fetchSendSMS({
            recNum: this.sendMessageData.userName,
            templateCode: this.SMStemplateValue,
            physiologicalDataId: this.sendMessageData.physiologicalDataId,
            chooseUser: "3"
          });
          this.sendChecked = true;
          this.showSendMessage = false;
          if (res.status == "200") {
            this.getUnusualInfo();
            this.SMStemplateValue = "";
            this.$message({
              type: "success",
              message: "发送成功"
            });
          } else {
            this.showSendMessage = false;
            this.$message({
              type: "error",
              message: "发送失败"
            });
          }
        } else if (this.SMStemplateStatus == "2") {
          const res = await fetchYouMengPushData({
            userId: this.sendMessageData.userId,
            pushMessageTemplateId: this.SMStemplateValue,
            physiologicalDataId: this.sendMessageData.physiologicalDataId,
            chooseUser: "3"
          });
          if (res.status == "200") {
            this.showSendMessage = false;
            this.getUnusualInfo();
            this.SMStemplateValue = "";
            this.$message({
              type: "success",
              message: "发送成功"
            });
          } else {
            this.showSendMessage = false;
            this.$message({
              type: "error",
              message: "发送失败"
            });
          }
        }
      }
    }
  },
  computed: {
    ...mapGetters(["permission_roles"])
  }
};
</script>

<style lang="less">
.SMS-ul {
  li {
    width: 400px;
    margin: 10px 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.sendMagin {
  width: 400px;
  margin: 0 auto;
}
.last {
  text-align: right;
}
</style>
